﻿using System;
using System.Web.UI.WebControls;
using Shuba.Model;
using System.Collections.Generic;
using DynamicComparer;

namespace Shuba.Web.bulk_upload
{
    public partial class maintenance_visit_list_bulk_failed : basePage
    {
        string msg = "";
        bool IsView, IsAdd, IsEdit, IsDelete, IsChecker;
        int rowsPerPage;
        string currentPage;
        int jmlRow;

        protected void Page_Load(object sender, EventArgs e)
        {
            Form.DefaultFocus = txtSearch.ClientID;
            Form.DefaultButton = btnSearch.UniqueID;

            string acl = GetUserAccess("MSTR_MAINTENANCE_VISIT");
            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);
            rowsPerPage = 20;

            if (!IsView)
            {
                /*
                msg = GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                */
            }

            if (!IsPostBack)
            {
                InitSort();
                ViewState["searchBy"] = "";
                currentPage = "1";
                ViewState["SortEx"] = "UPDATE_DATE";
                ViewState["Sort"] = "DESC";

                lblBatchID.Text = Request.QueryString["id"];

                BindData();

                LabelTitle.Text = String.Format("{0} Failed List", GetPageTitle("MSTR_MAINTENANCE_VISIT"));
            }
        }

        private void BindData()
        {
            Shuba.Business.MaintenanceVisitBulk BI = new Shuba.Business.MaintenanceVisitBulk();
            List<MaintenanceVisitBulkInfo> list; 

            if (ddlMaintenanceVisitPage.Items.Count == 0 || ViewState["searchBy"].ToString() != txtSearch.Text)
                currentPage = "1";
            else currentPage = ddlMaintenanceVisitPage.SelectedValue;

            jmlRow = BI.GetRowNumberMaintenanceVisitFailed(txtSearch.Text, lblBatchID.Text);

            list = BI.GetListMaintenanceVisitFailed(txtSearch.Text, lblBatchID.Text, int.Parse(currentPage), gvList.PageSize);
            ViewState["List"] = list;

            gvList.DataSource = list;
            gvList.DataBind();
            load_data_maintenance_visit_page();

            //if (IsDelete) gvList.Columns[gvList.Columns.Count - 1].Visible = true;
            //else gvList.Columns[gvList.Columns.Count - 1].Visible = false;
        }

        protected void load_data_maintenance_visit_page()
        {
            int jmlHal = (int)Math.Ceiling((decimal)jmlRow / (decimal)rowsPerPage);
            string pageNum;

            footLink.Visible = (jmlHal > 0);

            ddlMaintenanceVisitPage.Items.Clear();
            for (int i = 0; i < jmlHal; i++)
            {
                pageNum = (i + 1).ToString();
                ddlMaintenanceVisitPage.Items.Add(new ListItem(pageNum, pageNum));
            }

            if (ViewState["searchBy"].ToString() == txtSearch.Text)
                ddlMaintenanceVisitPage.SelectedValue = currentPage;
            ViewState["searchBy"] = txtSearch.Text;

            lblPage.Text = "Page " + currentPage + " of " + jmlHal;

            firstLink.Visible = true;
            prevLink.Visible = true;
            nextLink.Visible = true;
            lastLink.Visible = true;

            if (ddlMaintenanceVisitPage.SelectedIndex <= 0)
            {
                firstLink.Visible = false;
                prevLink.Visible = false;
            }
            if (ddlMaintenanceVisitPage.SelectedIndex >= ddlMaintenanceVisitPage.Items.Count - 1)
            {
                nextLink.Visible = false;
                lastLink.Visible = false;
            }
        }
        protected void ddlMaintenanceVisitPage_IndexChange(object sender, EventArgs e)
        {
            BindData();
        }

        protected void linkDataPage_Click(object sender, EventArgs e)
        {
            if (sender == firstLink)
                ddlMaintenanceVisitPage.SelectedIndex = 0;
            else if (sender == prevLink)
                ddlMaintenanceVisitPage.SelectedIndex--;
            else if (sender == nextLink)
                ddlMaintenanceVisitPage.SelectedIndex++;
            else if (sender == lastLink)
                ddlMaintenanceVisitPage.SelectedIndex = ddlMaintenanceVisitPage.Items.Count - 1;
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // untuk aksi delete sementara belum ada
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // bound row delete sesuai user privilege
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;

            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                List<MaintenanceVisitBulkInfo> list = (List<MaintenanceVisitBulkInfo>)ViewState["List"];

                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<MaintenanceVisitBulkInfo> comparer = new DynamicComparer<MaintenanceVisitBulkInfo>(sort);
                list.Sort(comparer);

                gvList.DataSource = list;
                gvList.DataBind();
            }
        }
    }
}